package org.tzeng.html.extra;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang.StringEscapeUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.tzeng.html.bean.*;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by tzeng on 2016-01-30.
 */
public class LYExtra {

    public static PageInfo extraGInfo(String file, TravelInfo travel){
        PageInfo pi = null;

        Document doc = Jsoup.parse(file, "UTF-8");
        // 日历价格
        /*String docStr = doc.toString();
        String detail = docStr.substring(docStr.indexOf("page_cf") + 10, docStr.indexOf("IsSingleRoomPrice") - 10).trim();

        LYPriceRoot priceO = JSON.parseObject(detail, LYPriceRoot.class);
        List<DPrice> prices = new ArrayList<DPrice>();
        for (PriceList pl : priceO.getPriceList()) {
            DPrice dp = new DPrice();
            dp.setDtime(pl.getDate().replaceAll("-", ""));
            dp.setPrice(Integer.parseInt(pl.getPrice()));
            prices.add(dp);
        }*/

        String toCity = doc.getElementById(travel.getProduct_to_city()).val();

        Elements today_price = doc.select(travel.getToday_price());
        String tPrice = "";
        if (today_price != null)
            tPrice = trimNum(today_price.text());

        Elements comment_wrap = doc.select(travel.getComment_wrap());
        double c_wrap = -1.0d;
        if (comment_wrap != null)
            c_wrap = Integer.parseInt(comment_wrap.html().matches("[0-9]{1,3}") ? comment_wrap.html() : "0") / 100d;

        Elements special_info = doc.select(travel.getSpecial_info());
        String s_info = "";
        if (special_info != null)
            s_info = special_info.text();

        Elements simple_route = doc.select(travel.getSimple_route());
        String s_route = "";
        if(simple_route != null)
            s_route = simple_route.text();

        Elements pm_recommend = doc.select(travel.getPm_recommend());
        String pr = "";
        if(pm_recommend != null)
            pr = pm_recommend.text().replaceAll(" ★", "\r\n").replaceAll("★", "");

        String titleStr = doc.title();
        Pattern pattern = Pattern.compile("[0-9]+日");
        Matcher matcher = pattern.matcher(titleStr);
        int days = 0;
        if (matcher.find())
            days = Integer.parseInt(matcher.group().replaceAll("日", ""));

        pi = new PageInfo();
        pi.setProduct_name(titleStr);
        pi.setProduct_to_city(toCity);
        pi.setProduct_num(doc.getElementById(travel.getProduct_num()).val());
        pi.setProduct_city(doc.getElementById(travel.getProduct_city()).val());
        pi.setProvider_href("");
        pi.setProvider_name("");
        pi.setSpecial_info(s_info);
        pi.setSimple_route(s_route);
        pi.setPm_recommend(pr);
        pi.setToday_price(Integer.parseInt(tPrice));
        pi.setComment_wrap(c_wrap);
        pi.setProduct_days(days);
//        pi.setPrices(prices);

        return pi;
    }

    public static String trimNum(String num){
        String result = "";
        for (int i = 0; i < num.length(); i ++){
            if(num.charAt(i)>=48 && num.charAt(i)<=57){
                result+=num.charAt(i);
            }
        }

        return result;
    }

}
